var prefix_path="http://123.56.219.116:8080/virtualcat/manage/cat/";
var cat_id = getQueryString("catId");

$(function () {
    ajax_findCatInfo();
    $("#catButton").click(function () {
        var formData = new FormData($('#catForm')[0]);
        formData.append("flag","cat");
        $.ajax({
            type: 'post',
            url: "http://localhost:8080/virtualcat/manage/cat/upload",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
        }).success(function (data) {
            if(data.data.msg=="login"){
                alert("请先登录");
                location.href="login.html"
            }
            console.info(data);
            $("span[name='cimg']").html("cat/"+data.toString());
            alert("上传成功");
        }).error(function () {
            alert("上传失败");
        });
    });
    $("#goodButton").click(function () {
        var formData = new FormData($('#goodForm')[0]);
        formData.append("flag","good");
        $.ajax({
            type: 'post',
            url: "http://localhost:8080/virtualcat/manage/cat/upload",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
        }).success(function (data) {
            if(data.data.msg=="login"){
                alert("请先登录");
                location.href="login.html"
            }
            console.info(data);
            $("span[name='gimg']").html("good/"+data.toString());
            alert("上传成功");
        }).error(function () {
            alert("上传失败");
        });
    });
});

function ajax_findCatInfo() {
    $.ajax({
        url: prefix_path+"searchCat",
        type:'GET',
        contentType:'application/json',
        dataType:"json",
        data:"catId="+cat_id,
    }).success(function (data) {
        if(data.data.msg=="login"){
            alert("请先登录");
            location.href="login.html";
            return;
        }
        console.info(data);
        $("span[name='catId']").html(data.data.catinfo.catid);
        $("input[name='cname']").val(data.data.catinfo.cname);
        $("span[name='cimg']").html(data.data.catinfo.cimg);
        $("input[name='gname']").val(data.data.catinfo.gname);
        $("span[name='gimg']").html(data.data.catinfo.gimg);
        $("input[name='nprice']").val(data.data.catinfo.nprice);
        $("input[name='oprice']").val(data.data.catinfo.oprice);
        $("textarea[name='cdesc']").val(data.data.catinfo.cdesc);
        $("textarea[name='gdesc']").val(data.data.catinfo.gdesc);
        $("input[name='detail']").val(data.data.catinfo.detail);
    }).error(function () {
        alert("添加失败");
    });
}

function ajax_update() {
    $.ajax({
        url: prefix_path+"updateCat",
        type:'POST',
        contentType:'application/json',
        dataType:"json",
        data: JSON.stringify({
            "catid":$("span[name='catId']").html(),
            "cname":$("input[name='cname']").val(),
            "cimg":$("span[name='cimg']").html(),
            "gname":$("input[name='gname']").val(),
            "gimg":$("span[name='gimg']").html(),
            "nprice":$("input[name='nprice']").val(),
            "oprice":$("input[name='oprice']").val(),
            "cdesc":$("textarea[name='cdesc']").val(),
            "gdesc":$("textarea[name='gdesc']").val(),
            "detail":$("input[name='detail']").val()
        }),
        success:function (data) {
            if(data.data.msg=="login"){
                alert("请先登录");
                location.href="login.html";
                return;
            }
            console.info(data);
            alert("修改成功");
        },
        error:function () {
            alert("修改失败");
        }
    });
}